<template>
    <div>
      <el-dialog
        center
        :title="isAdd ? '添加管理员' : '编辑管理员'"
        :visible.sync="isShow"
        :close-on-press-escape="false"
        :before-close="cancel"
      >
        <el-form :model="user_info" :rules="rules" ref="userinfo">
          <el-form-item
            prop="roleid"
            label="所属角色"
            :label-width="formLabelWidth"
          >
            <el-select v-model="user_info.roleid">
              <el-option
                v-for="item in rolelist"
                :key="item.id"
                :value="item.id"
                :label="item.rolename"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            prop="username"
            label="管理员名称"
            :label-width="formLabelWidth"
          >
            <el-input v-model="user_info.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input v-model="user_info.password" autocomplete="off"></el-input>
          </el-form-item>
          <p>密码为空，则不做修改</p>
          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-switch
              v-model="user_info.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="2"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="comfirm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { mapGetters, mapActions } from "vuex";
  //引入封装好的接口
  import {useradd,userinfo,useredit} from '../../request/api'
  export default {
    props: ["isShow", "isAdd"],
    data() {
      return {
        user_info: {
          roleid: "", //角色编号
          username: "", //管理员名称
          password: "", //密码
          status: 1, //状态   1正常2禁用   number类型
        },
        formLabelWidth: "120px",
        rules: {
          username: [
            { required: true, message: "请输入管理员名称", trigger: "blur" },
            {
              min: 2,
              max: 15,
              message: "长度在 2 到 15 个字符",
              trigger: "blur",
            },
          ],
          roleid: [
            { required: true, message: "请选择上级管理员", trigger: "change" },
          ],
        },
      };
    },
    computed: {
      ...mapGetters({
        rolelist: "role/getRole",
      }),
    },
    mounted() {
      //组件一加载 就调用角色行动
      if (this.rolelist.length == 0) {
        this.getRole();
      }
    },
    methods: {
      ...mapActions({
        getRole: "role/getRoleAction",
        getUserList:'user/getUserAction',
        getCount:'user/getCountAction'
      }),
      //封装一个取消事件
      cancel() {
        this.$emit("cancel", false);
        //并清空
        this.reset();
      },
      //封装一个重置事件
      reset() {
        this.user_info = {
          roleid: "", //角色编号
          username: "", //管理员名称
          password: "", //密码
          status: 1, //状态   1正常2禁用   number类型
        };
        //清空表单验证
        this.$refs.userinfo.resetFields();
      },
      //封装一个函数用来数据回显
      async lookup(uid) {
       let res = await userinfo({uid})
       this.user_info = res.list
       //注意： 如果密码为空则不做修改
       this.user_info.password =''
       this.user_info.uid = uid
      },
      //封装一个确定事件
      comfirm() {
        this.$refs.userinfo.validate((val) => {
          if (val) {
            //判断是否为添加还是编辑
            if (this.isAdd) {
              useradd(this.user_info)
              .then(res=>{
                if(res.code==200){
                  this.$message.success(res.msg)
                  this.cancel()
                  //获取列表
                  this.getUserList()
                  //获取总条数
                  this.getCount()
                }else{
                  this.$message.error(res.msg)
                }
              })
            } else {
              useredit(this.user_info)
              .then(res=>{
                if(res.code==200){
                  this.$message.success(res.msg)
                  this.cancel()
                  //获取列表
                  this.getUserList()
                }else{
                  this.$message.error(res.msg)
                }
              })
            }
          }
        });
      },
    },
  };
  </script>
  
  <style scoped></style>
  